from selenium.webdriver.common.by import By
from pages.base_page import BasePage


class ZnxPage(BasePage):
    url = "http://192.168.1.55/crm/index.php?m=message&a=index"
    zns_page_success_locator = (By.XPATH,"/html/body/div[5]/div[2]/button")
    delete_page_success_locator = (By.XPATH, "/html/body/div[5]/div[2]/button")
    # 清楚数据定位表格
    tbody_locator = (By.CSS_SELECTOR, "#form1 > table > tbody")
    plcz_locator = (
    By.CSS_SELECTOR, "body > div.container > div.row > div > div.span12.nav > div > ul > li.nav.pull-left > div > a")
    delate_locator = (By.ID, "delete_receive")
    add_xx_locator = (By.LINK_TEXT, "写信")
    sjr_locator = (By.XPATH, '//*[@id="ta1"]/span/input')
    neirong_locator = (By.NAME, "content")
    fasong_locator = (By.XPATH, '//*[@id="dialog-message-send"]/form/table/tfoot/tr/td[2]/input[1]')

    def xx_click(self):
        """点击写信按钮"""
        self.driver.find_element(*self.add_xx_locator).click()

    def sjr_click(self):
        """收件人选择框"""
        self.driver.find_element(*self.sjr_locator).click()

    def neirong_input(self,content):
        """内容输入框"""
        self.driver.find_element(*self.neirong_locator).send_keys(content)
    def fasong_click(self):
        """点击保存"""
        self.driver.find_element(*self.fasong_locator).click()

    def get_znxps_text(self):
        return self.driver.find_element(*self.zns_page_success_locator).text

    def delete_click(self,name):
        """清楚数据"""
        tbody = self.driver.find_element(*self.tbody_locator)
        trs = tbody.find_elements(By.TAG_NAME, "tr")
        for i in trs:
            tds = i.find_elements(By.TAG_NAME, 'td')
            if tds[1].text == name:
                tds[0].find_element(By.CSS_SELECTOR, 'input[name="message_id[]"]').click()
                break
        self.driver.find_element(*self.plcz_locator).click()
        self.driver.find_element(*self.delate_locator).click()
        self.driver.switch_to.alert.accept()

        def get_znx_delete_text(self):
            return self.driver.find_element(*self.delete_page_success_locator).text

    def open(self):
        """打开页面地址"""
        self.driver.get(self.url)